/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlisinhvien.pl;


import java.awt.event.ItemEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import quanlisinhvien.bll.KhoaBLL;
import quanlisinhvien.bll.LopBLL;
import quanlisinhvien.bll.SinhVienBLL;
import quanlisinhvien.dto.*;
/**
 *
 * @author Marshmallow
 */
public class F_QuanLiSinhVien extends javax.swing.JFrame {

   
    KhoaBLL qlKhoa;
    List<KhoaDTO> danhSachKhoa;
    LopBLL qlLop;
    List<LopDTO> danhSachLop;
    SinhVienBLL qlSinhVien;
    SinhVienDTO sinhVien;
    static List<SinhVienDTO> danhSachSinhVien;
    boolean daTimKiem; //xác định danh sách sinhvien được load vào table
    final Locale locale = getLocale("");
    int dongHienHanh=- 1; //xác định dòng hiện hành để xác định sinhvien được chọn từ listSinhVien
    
    /**
     * Creates new form F_QuanLiSinhVien
     */
    public F_QuanLiSinhVien() {
        initComponents();
        
        qlKhoa = new KhoaBLL();
        qlLop = new LopBLL();
        qlSinhVien = new SinhVienBLL();
        danhSachKhoa = new ArrayList<KhoaDTO>();
        danhSachLop = new ArrayList<LopDTO>();
        danhSachSinhVien = new ArrayList<SinhVienDTO>();
        daTimKiem = false;
        
        HienThiBangSinhVien(daTimKiem);
        HienThiDanhSachKhoa();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroupGioiTinh = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtMaSinhVien = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtHoTen = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cboLop = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        rbtnNam = new javax.swing.JRadioButton();
        rbtnNu = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtDiaChi = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cboKhoa = new javax.swing.JComboBox();
        btnChonNgay = new javax.swing.JButton();
        txtNgaySinh = new quanlisinhvien.pl.ObservingTextField();
        jPanel4 = new javax.swing.JPanel();
        btnThem = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblSinhVien = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        btnTimKiem = new javax.swing.JButton();
        btnLamMoi = new javax.swing.JButton();
        btnXemDiem = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Quản Lý Sinh Viên");
        setBounds(new java.awt.Rectangle(200, 50, 0, 0));
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Mã sinh viên:");

        txtMaSinhVien.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Họ tên:");

        txtHoTen.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Lớp:");

        cboLop.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cboLop.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("Giới tính:");

        btnGroupGioiTinh.add(rbtnNam);
        rbtnNam.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        rbtnNam.setSelected(true);
        rbtnNam.setText("Nam");

        btnGroupGioiTinh.add(rbtnNu);
        rbtnNu.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        rbtnNu.setText("Nữ");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("Ngày sinh:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setText("Địa chỉ:");

        txtDiaChi.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Khoa:");

        cboKhoa.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cboKhoa.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboKhoaItemStateChanged(evt);
            }
        });

        btnChonNgay.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnChonNgay.setText("Ngày");
        btnChonNgay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChonNgayActionPerformed(evt);
            }
        });

        txtNgaySinh.setFocusable(false);
        txtNgaySinh.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txtNgaySinh.setName("txtNgaySinh"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel8))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cboLop, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtMaSinhVien, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboKhoa, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(txtDiaChi, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addComponent(rbtnNam)
                                            .addGap(18, 18, 18)
                                            .addComponent(rbtnNu)
                                            .addGap(36, 36, 36)
                                            .addComponent(jLabel7)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(txtNgaySinh, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))
                                    .addGap(10, 10, 10)
                                    .addComponent(btnChonNgay))))))
                .addGap(62, 62, 62))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboKhoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboLop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(9, 9, 9)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMaSinhVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(rbtnNam)
                    .addComponent(rbtnNu)
                    .addComponent(jLabel7)
                    .addComponent(btnChonNgay)
                    .addComponent(txtNgaySinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        btnThem.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnSua.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnSua.setText("Sửa");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnSua, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnSua)
                    .addComponent(btnXoa))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblSinhVien.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tblSinhVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblSinhVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblSinhVienMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblSinhVien);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        btnTimKiem.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnTimKiem.setText("Tìm kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        btnLamMoi.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnLamMoi.setText("Làm mới");
        btnLamMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLamMoiActionPerformed(evt);
            }
        });

        btnXemDiem.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        btnXemDiem.setText("Xem điểm");
        btnXemDiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXemDiemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnTimKiem)
                .addGap(18, 18, 18)
                .addComponent(btnLamMoi)
                .addGap(18, 18, 18)
                .addComponent(btnXemDiem)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTimKiem)
                    .addComponent(btnLamMoi)
                    .addComponent(btnXemDiem))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        if (txtMaSinhVien.getText().equals("")){
            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập mã sinh viên");
            return;
        }
        if (txtNgaySinh.getText().equals("")){
            JOptionPane.showMessageDialog(rootPane, "Vui lòng nhập ngày sinh");
            return;
        }
        
        SinhVienDTO svInsert = new SinhVienDTO();
        svInsert.setMaSinhVien(txtMaSinhVien.getText());
        NhanThongTin(svInsert);
        
        if (qlSinhVien.ThemSinhVien(svInsert))
            JOptionPane.showMessageDialog(rootPane, "Thêm thành công");
        else
            JOptionPane.showMessageDialog(rootPane, "Thêm không thành công");
        
        HienThiBangSinhVien(daTimKiem);
    }//GEN-LAST:event_btnThemActionPerformed

    //Sự kiện chọn dòng ở bảng Sinh Viên
    private void tblSinhVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSinhVienMouseClicked
        //Xác định dòng được chọn từ bảng
        dongHienHanh = tblSinhVien.getSelectedRow();  
        if (dongHienHanh >= 0){
            sinhVien = danhSachSinhVien.get(dongHienHanh);
            HienThiChiTietSinhVien(sinhVien);
        }
    }//GEN-LAST:event_tblSinhVienMouseClicked
       
    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        SinhVienDTO svUpdate = new SinhVienDTO(sinhVien);
        NhanThongTin(svUpdate);
        if (qlSinhVien.CapNhatSinhVien(svUpdate))
            JOptionPane.showMessageDialog(rootPane, "Cập nhật thành công");
        else
            JOptionPane.showMessageDialog(rootPane, "Cập nhật không thành công");
        HienThiBangSinhVien(daTimKiem);
    }//GEN-LAST:event_btnSuaActionPerformed

    private void btnXemDiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXemDiemActionPerformed
        if (!txtMaSinhVien.getText().equals("")){    
            F_DiemSinhVien fDiem = new F_DiemSinhVien(new JFrame(),qlSinhVien.LaySinhVienTheoMa(sinhVien.getMaSinhVien()));          
            JDialog dialog = fDiem;            
            dialog.show();
            
            HienThiBangSinhVien(daTimKiem);
        }
        else
            JOptionPane.showMessageDialog(rootPane, "Vui lòng chọn sinh viên");
    }//GEN-LAST:event_btnXemDiemActionPerformed

    private void cboKhoaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboKhoaItemStateChanged
        Object item = evt.getItem();
        
        if(evt.getStateChange()  == ItemEvent.SELECTED){
            HienThiDanhSachLop(item.toString());
        }
        
    }//GEN-LAST:event_cboKhoaItemStateChanged

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        if ((dongHienHanh != -1) && qlSinhVien.XoaSinhVien(txtMaSinhVien.getText())){
            JOptionPane.showMessageDialog(rootPane, "Xóa thành công");
            danhSach.removeRow(dongHienHanh);
            dongHienHanh--;
            HienThiBangSinhVien(daTimKiem);
            if (dongHienHanh >= 0){
                sinhVien = danhSachSinhVien.get(dongHienHanh);
                HienThiChiTietSinhVien(sinhVien);
            }
        }
        else if (!qlSinhVien.XoaSinhVien(txtMaSinhVien.getText())){
            JOptionPane.showMessageDialog(rootPane, "Xóa không thành công");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        F_TimKiem formTimKiem = new F_TimKiem(new JFrame());
        JDialog dialog = formTimKiem;
        dialog.show();
        
        daTimKiem = true;
        dongHienHanh = -1;
        HienThiBangSinhVien(daTimKiem);
        
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnLamMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLamMoiActionPerformed
        daTimKiem = false;
        dongHienHanh = -1;
        HienThiBangSinhVien(daTimKiem);
    }//GEN-LAST:event_btnLamMoiActionPerformed

    //Sử dụng với datepicker
    private void btnChonNgayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChonNgayActionPerformed
       String lang = null;
     
       DatePicker dp = new DatePicker(txtNgaySinh, locale); //hiện ngày từ textfield vào lịch
       Date selectedDate = dp.parseDate(txtNgaySinh.getText()); //hiện thị ngày vào textfield, chỉnh format ngày tại đây
       
       dp.setSelectedDate(selectedDate);
       dp.start(txtNgaySinh);
    }//GEN-LAST:event_btnChonNgayActionPerformed

    private Locale getLocale(String loc){
        if ((loc != null) && (loc.length() > 0)){
            return new Locale(loc);
        }
        else
            return Locale.US;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(F_QuanLiSinhVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(F_QuanLiSinhVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(F_QuanLiSinhVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(F_QuanLiSinhVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new F_QuanLiSinhVien().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChonNgay;
    private javax.swing.ButtonGroup btnGroupGioiTinh;
    private javax.swing.JButton btnLamMoi;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JButton btnXemDiem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cboKhoa;
    private javax.swing.JComboBox cboLop;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbtnNam;
    private javax.swing.JRadioButton rbtnNu;
    private javax.swing.JTable tblSinhVien;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtHoTen;
    private javax.swing.JTextField txtMaSinhVien;
    private quanlisinhvien.pl.ObservingTextField txtNgaySinh;
    // End of variables declaration//GEN-END:variables

    DefaultTableModel danhSach;
    private void HienThiBangSinhVien(boolean daTimKiem) {
        //Sử dụng Vector để thêm các tên cột (header)
        Vector thuocTinh = new Vector();
        thuocTinh.add("Mã SV");
        thuocTinh.add("Họ Tên");
        thuocTinh.add("Giới tính");
        thuocTinh.add("Ngày sinh");
       
        Vector row;
        DefaultTableModel model = new DefaultTableModel(thuocTinh, 0);     
        if (!daTimKiem)
           danhSachSinhVien = qlSinhVien.LayDanhSachSinhVien();
        
        for (SinhVienDTO sv : danhSachSinhVien){
            row = new Vector();
            row.addElement(sv.getMaSinhVien());
            row.addElement(sv.getHoTen());
            row.addElement(sv.getGioiTinh());
            row.addElement(sv.getNgaySinh());
            model.addRow(row);
        }
        
        danhSach = model;
        tblSinhVien.setModel(model);
        if (model.getRowCount() == 0){
            dongHienHanh = -1;
        }
        
        if (dongHienHanh!=-1){
            tblSinhVien.setRowSelectionInterval(dongHienHanh, dongHienHanh);
        }
        else
            XoaChiTiet();
    }

    private void HienThiDanhSachKhoa() {       
        danhSachKhoa = qlKhoa.LayDanhSachKhoa();
        String[] dsTenKhoa = new String[danhSachKhoa.size()];
        for (int i = 0; i<dsTenKhoa.length; i++){
            //cboKhoa.addItem(k.getTenKhoa());           
            dsTenKhoa[i] = danhSachKhoa.get(i).getTenKhoa();
        }
        DefaultComboBoxModel model = new DefaultComboBoxModel(dsTenKhoa);
        cboKhoa.setModel(model);
        if (dsTenKhoa.length>0){
            HienThiDanhSachLop(model.getElementAt(0).toString());
        }
    }
    
    //Thay đổi list lớp khi chọn qlKhoa
    private void HienThiDanhSachLop(String tenKhoa){        
        //cboLop.removeAllItems();
        danhSachLop = qlLop.LayDanhSachLopTheoKhoa(tenKhoa);
        String[] dsTenLop = new String[danhSachLop.size()];
        for (int i = 0; i<dsTenLop.length; i++){           
            dsTenLop[i] = danhSachLop.get(i).getTenLop();
        }
        DefaultComboBoxModel model = new DefaultComboBoxModel(dsTenLop);
        cboLop.setModel(model);
    }
    
    //Hiển thị chi tiết của 1 sinh viên khi chọn ở table SinhVien
    private void HienThiChiTietSinhVien(SinhVienDTO sv){
        
            //Xác định sinh viên được chọn theo thứ tự trên
           

           //Hiển thị thông tin sinh viên 
           txtMaSinhVien.setText(sv.getMaSinhVien());
           txtHoTen.setText(sv.getHoTen());
           txtNgaySinh.setText(sv.getNgaySinh().toString());
           txtDiaChi.setText(sv.getDiaChi());
           
           //Xóa chọn ở nhóm radiobutton giới tính trước khi set lại
           btnGroupGioiTinh.clearSelection();
           if(sv.getGioiTinh().equals("Nam")){
               rbtnNam.setSelected(true);
           }
           else
               rbtnNu.setSelected(true);
           //Hiển thị qlKhoa và lớp của sinh viên 
           cboKhoa.setSelectedItem(sv.getLop().getKhoa().getTenKhoa());
           cboLop.setSelectedItem(sv.getLop().getTenLop());   

    }
    
    //Nhận thông tin của một sinh viên được dùng cho việc thêm và sửa
    private void NhanThongTin(SinhVienDTO sv){
        sv.setHoTen(txtHoTen.getText());
        sv.setDiaChi(txtDiaChi.getText());
        sv.setGioiTinh(rbtnNam.isSelected()? "Nam" : "Nữ");
        DatePicker dp = new DatePicker(txtNgaySinh, locale); 
        Date selectedDate =dp.parseDate(txtNgaySinh.getText());        
        sv.setNgaySinh(UtilDateToSQLDate(selectedDate));
        for (LopDTO l : danhSachLop){
            if (l.getTenLop() == cboLop.getSelectedItem())
                sv.setLop(l);
        }
    }
    
    //Xóa trắng các textbox chi tiết trên giao diện
    private void XoaChiTiet(){
        txtMaSinhVien.setText("");
        txtHoTen.setText("");
        txtDiaChi.setText("");
        txtNgaySinh.setText("");
    }
    
    private java.sql.Date UtilDateToSQLDate(java.util.Date utilDate ){
        java.sql.Date sqlDate = null;
        if (utilDate != null)
            sqlDate = new java.sql.Date(utilDate.getTime());
        return sqlDate;
    }
}
